include ../upgradeable_versions.mk

EXTENSION     = postgis
EXTVERSION    = @POSTGIS_LIB_VERSION@
MINORVERSION  = @POSTGIS_MAJOR_VERSION@.@POSTGIS_MINOR_VERSION@
GREP = @GREP@
PERL = @PERL@

MICRO_NUMBER  = $(shell echo $(EXTVERSION) | \
						$(PERL) -pe 's/\d.\d.(\d+)[a-zA-Z]*\d*/$1/'

PREREL_NUMBER = $(shell echo $(EXTVERSION) | \
                        $(PERL) -pe 's/\d\.\d\.(.*)/\1/' | \
                        $(GREP) "[a-zA-Z]" | \
                        $(PERL) -pe 's/\d+[a-zA-Z]+(\d+)/\1/'

MICRO_PREV    = $(shell if test "$(MICRO_NUMBER)x" != "x"; then expr $(MICRO_NUMBER) - 1; fi)
PREREL_PREV   = $(shell if test "$(PREREL_NUMBER)x" != "x"; then expr $(PREREL_NUMBER) - 1; fi)

PREREL_PREFIX = $(shell echo $(EXTVERSION) | \
                        $(PERL) -pe 's/\d\.\d\.(.*)/\1/' | \
                        $(GREP) "[a-zA-Z]" | \
                        $(PERL) -pe 's/(\d+[a-zA-Z]+)\d*/\1/'

DATA         = $(filter-out $(wildcard sql/*--*.sql),$(wildcard sql/*.sql))

#DOCS         = $(wildcard ../../doc/html/*.html)
PG_CONFIG    = @PG_CONFIG@

PG91         = $(shell $(PG_CONFIG) --version | $(GREP) -qE " 8\.| 9\.0" && echo no || echo yes)
SQL_BITS     = $(wildcard sql_bits/*.sql)
EXTRA_CLEAN += ${SQL_BITS} sql/*.sql

CURV_big=@POSTGIS_MAJOR_VERSION@@POSTGIS_MINOR_VERSION@

ifeq ($(PG91),yes)
	
all: sql/$(EXTENSION)--$(EXTVERSION).sql sql/$(EXTENSION)--unpackaged--$(EXTVERSION).sql sql/$(EXTENSION)--$(EXTVERSION)--$(EXTVERSION)next.sql sql/$(EXTENSION)--$(EXTVERSION)next--$(EXTVERSION).sql sql_minor_upgrade

sql/$(EXTENSION).sql: sql_bits/postgis.sql  sql_bits/spatial_ref_sys_config_dump.sql  sql_bits/spatial_ref_sys.sql
	mkdir -p sql
	printf '\\echo Use "CREATE EXTENSION $(EXTENSION)" to load this file. \\quit\n' > $@
	cat $^ >> $@

sql/$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).sql
	mkdir -p sql
	cp $< $@

sql/$(EXTENSION)--unpackaged--$(EXTVERSION).sql: sql_bits/postgis.sql sql_bits/rtpostgis.sql ../../utils/create_unpackaged.pl
	mkdir -p sql
	cat ../../postgis/postgis.sql ../../raster/rt_pg/rtpostgis.sql | $(PERL) ../../utils/create_unpackaged.pl postgis > $@
	
#this is a cludge to allow upgrading from same SVN to same SVN
sql/$(EXTENSION)--$(EXTVERSION)--$(EXTVERSION)next.sql: sql_bits/postgis_extension_upgrade_minor.sql
	cp $< $@
sql/$(EXTENSION)--$(EXTVERSION)next--$(EXTVERSION).sql: sql_bits/postgis_extension_upgrade_minor.sql
	cp $< $@

#strip BEGIN/COMMIT since these are not allowed in extensions
sql_bits/spatial_ref_sys.sql: ../../spatial_ref_sys.sql	
	$(PERL) -pe 's/BEGIN\;//g ; s/COMMIT\;//g' $< > $@

#strip BEGIN/COMMIT since these are not allowed in extensions
sql_bits/postgis.sql: ../../postgis/postgis_for_extension.sql
	mkdir -p sql_bits/
	$(PERL) -pe 's/BEGIN\;//g ; s/COMMIT\;//g' $< > $@

../../doc/postgis_comments.sql:
	$(MAKE) -C ../../doc comments

sql_bits/postgis_comments.sql: ../../doc/postgis_comments.sql
	cp $< $@
		
#strip BEGIN/COMMIT since these are not allowed in extensions
sql_bits/rtpostgis.sql: ../../raster/rt_pg/rtpostgis_for_extension.sql
	mkdir -p sql_bits
	$(PERL) -pe 's/BEGIN\;//g ; s/COMMIT\;//g' $< > $@

sql_bits/spatial_ref_sys_config_dump.sql: ../../spatial_ref_sys.sql ../../utils/create_spatial_ref_sys_config_dump.pl
	$(PERL) ../../utils/create_spatial_ref_sys_config_dump.pl $< > $@

sql_bits/rtpostgis_upgrade.sql: ../../raster/rt_pg/rtpostgis_upgrade_for_extension.sql 
	$(PERL) -pe "s/BEGIN\;//g ; s/COMMIT\;//g; s/^(DROP .*)\;/SELECT postgis_extension_drop_if_exists('$(EXTENSION)', '\1');\n\1\;/" $< > $@

sql_bits/postgis_upgrade.sql: ../../postgis/postgis_upgrade_for_extension.sql
	$(PERL) -pe "s/BEGIN\;//g ; s/COMMIT\;//g; s/^(DROP .*)\;/SELECT postgis_extension_drop_if_exists('$(EXTENSION)', '\1');\n\1\;/" $< > $@


../../doc/raster_comments.sql:
	$(MAKE) -C ../../doc comments

sql_bits/raster_comments.sql: ../../doc/raster_comments.sql
	cp $< $@

#postgis_extension_upgrade_minor.sql is the one that contains both postgis AND raster
sql_bits/postgis_extension_upgrade_minor.sql: ../postgis_extension_helper.sql sql_bits/postgis_upgrade.sql sql_bits/rtpostgis_upgrade.sql ../../doc/raster_comments.sql ../../doc/postgis_comments.sql ../postgis_extension_helper_uninstall.sql
	printf '\\echo Use "CREATE EXTENSION $(EXTENSION)" to load this file. \\quit\n' > $@
	cat $^ >> $@

sql_minor_upgrade: sql_bits/postgis_extension_upgrade_minor.sql
	for OLD_VERSION in $(UPGRADEABLE_VERSIONS); do \
  	  cat $< > sql/$(EXTENSION)--$$OLD_VERSION--$(EXTVERSION).sql; \
	done

DATA = $(wildcard sql/*--*.sql)
EXTRA_CLEAN += sql/$(EXTENSION)--$(EXTVERSION).sql sql/postgis--unpackaged--$(EXTVERSION).sql
endif

distclean: clean
	rm -f Makefile

PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
PERL = @PERL@
